package mybatisTest.util;

//根据前序遍历和中序遍历得到 按层打印
import java.util.*;
public class TreeTraverse {


    public static int[] pre=null;
    public static int[] mmm=null;
    public static Map<Integer,Integer> map=new TreeMap<>();
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n = scanner.nextInt();
        pre=new int[n];mmm=new int[n];
        for (int i = 0; i <n; i++) {
            pre[i]=scanner.nextInt();
        }
        for (int i = 0; i <n; i++) {
            mmm[i]=scanner.nextInt();
        }
        subf(0,n-1,0,n-1,1);

        Set<Integer> keys = map.keySet();
        for (Integer key:keys){
            System.out.print(map.get(key)+" ");
        }
    }
    public static void subf(int l1,int r1,int l2,int r2,int index){

        int head=pre[l1];int pos2=0;
        map.put(index,head);
        for (int i = l2; i <=r2 ; i++) {
            if(mmm[i]==head){
                pos2=i;break;
            }
        }
        int leftlength=pos2-l2;int rightlength=r2-pos2;
        if(leftlength>=1){
            subf(l1+1,l1+leftlength,l2,pos2-1,2*index);
        }
        if(rightlength>=1){
            subf(r1+1-rightlength,r1,pos2+1,r2,2*index+1);
        }
    }
}
